/**
 * author:Administrator
 * date:2016/3/2
 * desc:TODO
 */
"use strict";

let Router = require('koa-router')
  , __pathPrefix = '/api/v1/role'
  , router = new Router({prefix: __pathPrefix});
module.exports = router.routes();
console.log('router:' + __pathPrefix + ',file=' + __filename);

let koabody = require('koa-better-body')();
let roleService = require('../../ddd/application/roleService.js');
let queryService = require('../../ddd/application/queryService');
let appUtil = require('../../common/appUtil.js');
let FormatOutput = require('../../common/FormatOutput.js');
let FormatError = require('../../common/FormatError.js');

/**
 * 获取登录用户 的 role
 */
router.get('/', function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let roles = yield queryService.allUserRole(user.id);
  return this.body = new FormatOutput(0, null, roles);
});

/**
 * 用户选择一个role 做为登录role
 * get /api/v1/role/role/use?type=manager&id=1
 */
router.get('/use', function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let type = this.query.type;
  let id = this.query.id;
  let uId = user.id;
  let role = yield queryService.getRole(type, uId, id);
  if (role) {
    role.type = type;
    yield appUtil.saveRole(this, role);
    this.body = new FormatOutput(0, {role: role});
  } else {
    this.body = FormatOutput.payloadOrError(null, FormatError.E_role_not_exist);
  }
});

/**
 * 已登录用户,创建一个 customer
 * name,token
 */
router.post('/customer', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let fields = this.request.body.fields;
  let name = fields.name;

  let result = yield roleService.registerCustomer({name: name, uId: user.id});
  this.body = FormatOutput.payloadOrError(result, FormatError.E_register_customer);
});

/**
 * 已登录用户,创建一个 vendor
 * name,token
 */
router.post('/vendor', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);
  let fields = this.request.body.fields;
  let name = fields.name;

  let result = yield roleService.registerVendor({name: name, uId: user.id});
  this.body = FormatOutput.payloadOrError(result, FormatError.E_register_customer);
});

/**
 * 已登录用户,创建一个 manager
 * name,token
 */
router.post('/manager', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  // 权限检测:必须是已登录manager操作
  if (!user.role || user.role.type != 'manager') {
    return this.body = FormatOutput.payloadOrError(null, FormatError.E_permission_deny);
  }

  let fields = this.request.body.fields;
  let name = fields.name;
  let result = yield roleService.registerManager({name: name, uId: user.id});
  this.body = FormatOutput.payloadOrError(result, FormatError.E_register_customer);

});

/**
 * 已登录用户,创建一个 shopkeeper
 * name,token
 */
router.post('/shopkeeper', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let fields = this.request.body.fields;
  let name = fields.name;
  // 权限检测:
  if (user.role && user.role.type == 'vendor') {
    // 1 必须是已登录vendor操作,keeper的vendorId 确定
    let vendorId = user.role.id;
    let result = yield roleService.registerKeeper({name: name, uId: user.id, vendorId: vendorId});
    return this.body = FormatOutput.payloadOrError(result, FormatError.E_register_customer);
  } else if (user.role && user.role.type == 'manager') {
    // 2 manager 插入数据,keeper无 vendorId
    let result = yield roleService.registerKeeper({name: name, uId: user.id});
    return this.body = FormatOutput.payloadOrError(result, FormatError.E_register_customer);
  }
  return this.body = FormatOutput.payloadOrError(null, FormatError.E_permission_deny);
});